<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车辆类型" prop="vehicleType">
        <el-select v-model="queryParams.vehicleType" placeholder="车辆类型" clearable>
          <el-option
            v-for="dict in dict.type.lg_vehicle_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="车辆长度" prop="vehicleLength">
        <el-input
          v-model="queryParams.vehicleLength"
          placeholder="请输入车辆长度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车辆容积" prop="vehicleVolume">
        <el-input
          v-model="queryParams.vehicleVolume"
          placeholder="请输入车辆容积"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌号" prop="licensePlate">
        <el-input
          v-model="queryParams.licensePlate"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车辆状态" prop="vehicleStatus">
        <el-select v-model="queryParams.vehicleStatus" placeholder="车辆状态" clearable>
          <el-option
            v-for="dict in dict.type.equipment_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- 创建时间范围选择 -->
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格数据 -->
    <el-table v-loading="loading" :data="vehicleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />

      <el-table-column label="车辆类型" align="center" prop="vehicleType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.lg_vehicle_type" :value="scope.row.vehicleType"/>
        </template>
      </el-table-column>
      <el-table-column label="车辆长度(m)" align="center" prop="vehicleLength" />
      <el-table-column label="车辆容积(m³)" align="center" prop="vehicleVolume" />
      <el-table-column label="车牌号" align="center" prop="licensePlate" />
      <el-table-column label="车辆状态" align="center" prop="vehicleStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.equipment_status" :value="scope.row.vehicleStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit"  v-hasPermi="['equipment:temperature:updateVehicle']" @click="submitForm(scope.row)">选择</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getListVehicle"
    />
  </div>
</template>

<script>
import {updateVehicle,listVehicle } from "@/api/equipment/temperature"
import store from "@/store";

export default {
  name: "Vehicle",
  props: {
    tid: {
      type: [Number, String], // 根据实际情况调整类型
      default: null
    }
  },
  dicts: ['sys_normal_disable',
  'lg_vehicle_type'],
  data() {
    return {
      userId: store.state.user.id, // 获取缓存的用户id
      userName: store.state.user.name, // 获取缓存的用户名
      loading: false,
      vehicleList: [],
      dateRange: [],
      total: 0,
      showSearch: true,
      dialogFormVisible: false,
      isEditMode: false,
      form: {
        vehicleType: '',
        vehicleLength: '',
        vehicleVolume: '',
        licensePlate: '',
        vehicleStatus: '',
        createTime: '', // 创建时间字段
        createBy: '', // 创建者字段
        updateBy: '' // 修改者字段
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vehicleType: undefined,
        vehicleLength: undefined,
        vehicleVolume: undefined,
        licensePlate: undefined,
        beginTime: undefined,
        endTime: undefined,
        vehicleStatus: undefined
      }
    };
  },
  created() {
    this.getListVehicle();
  },
  methods: {
    getListVehicle() {
      this.loading = true;
      listVehicle(this.addDateRange(this.queryParams,this.dateRange)).then(response => {
        console.log(response)
        this.vehicleList = response.rows;
        this.total = response.total;
        this.loading = false;
      }).catch(() => {
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getListVehicle();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.getTotalSome();
    },
    //获取数据总条数
    getTotalSome() {
      this.loading = true;
      getTotal(this.queryParams).then(response => {
        this.getVehicleList();
        this.total = response.data;
        this.loading = false;
      });
      this.loading = false;
    },

    submitForm(row) {
      updateVehicle({
        tid: this.tid,
        vehicleId: row.vehicleId,
        updateBy: this.$store.state.user.name,
      }).then(() => {
        this.$message.success('绑定成功');
        this.$emit('selected', row.vehicleId);
        // this.$emit('close'); // 关键代码：触发自定义事件
      }).catch(() => {
        this.$message.error('绑定失败');
      });
    }
  }
};
</script>
